﻿using cn.jpush.api.push.mode;
using cn.jpush.api.util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyPush
{
    public partial class PushTargetForm : Form
    {
        public Audience Target
        {
            get
            {
                return this.audience;
            }
        }

        private Audience audience;
        public PushTargetForm(Audience audience)
        {
            InitializeComponent();
            this.audience = audience;
        }

        private void PushTargetForm_Load(object sender, EventArgs e)
        {
            if (this.audience == null)
                this.audience = Audience.all();
            if (this.audience.dictionary != null)
            {
                if (this.audience.dictionary.ContainsKey("tag"))
                {
                    HashSet<string> hash1 = this.audience.dictionary["tag"];
                    tTag.Tag = hash1;
                    if (hash1 != null)
                        tTag.Text = JsonTool.ObjectToJson(hash1);
                }
                if (this.audience.dictionary.ContainsKey("tag_and"))
                {
                    HashSet<string> hash1 = this.audience.dictionary["tag_and"];
                    tTag_And.Tag = hash1;
                    if (hash1 != null)
                        tTag_And.Text = JsonTool.ObjectToJson(hash1);
                }
                if (this.audience.dictionary.ContainsKey("alias"))
                {
                    HashSet<string> hash1 = this.audience.dictionary["alias"];
                    tAlias.Tag = hash1;
                    if (hash1 != null)
                        tAlias.Text = JsonTool.ObjectToJson(hash1);
                }
                if (this.audience.dictionary.ContainsKey("registration_id"))
                {
                    HashSet<string> hash1 = this.audience.dictionary["registration_id"];
                    tRegistration.Tag = hash1;
                    if (hash1 != null)
                        tRegistration.Text = JsonTool.ObjectToJson(hash1);
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool isValid = false;
            HashSet<string> hashs1 = tTag.Tag as HashSet<string>;
            if (hashs1 != null)
            {
                isValid = true;
                this.audience.tag(hashs1);
            }
            HashSet<string> hashs2 = tTag_And.Tag as HashSet<string>;
            if (hashs2 != null)
            {
                isValid = true;
                this.audience.tag_and(hashs2);
            }
            HashSet<string> hashs4 = tAlias.Tag as HashSet<string>;
            if (hashs4 != null)
            {
                isValid = true;
                this.audience.alias(hashs4);
            }
            HashSet<string> hashs5 = tRegistration.Tag as HashSet<string>;
            if (hashs5 != null)
            {
                isValid = true;
                this.audience.registrationId(hashs5);
            }
            if (!isValid)
            {
                MessageBox.Show("请至少设置一项目标！", "提示");
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnTag_Click(object sender, EventArgs e)
        {
            HashSet<string> hashs = tTag.Tag as HashSet<string>;
            using (HashSetForm hsf = new HashSetForm(hashs))
            {
                if (hsf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    tTag.Tag = hsf.Hashs;
                    if (hsf.Hashs != null && hsf.Hashs.Count > 0)
                        tTag.Text = JsonTool.ObjectToJson(hsf.Hashs);
                    else
                        tTag.Text = "";
                }
            }
        }

        private void btnTag_And_Click(object sender, EventArgs e)
        {
            HashSet<string> hashs = tTag_And.Tag as HashSet<string>;
            using (HashSetForm hsf = new HashSetForm(hashs))
            {
                if (hsf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    tTag_And.Tag = hsf.Hashs;
                    if (hsf.Hashs != null && hsf.Hashs.Count > 0)
                        tTag_And.Text = JsonTool.ObjectToJson(hsf.Hashs);
                    else
                        tTag_And.Text = "";
                }
            }
        }

        private void btnAlias_Click(object sender, EventArgs e)
        {
            HashSet<string> hashs = tAlias.Tag as HashSet<string>;
            using (HashSetForm hsf = new HashSetForm(hashs))
            {
                if (hsf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    tAlias.Tag = hsf.Hashs;
                    if (hsf.Hashs != null && hsf.Hashs.Count > 0)
                        tAlias.Text = JsonTool.ObjectToJson(hsf.Hashs);
                    else
                        tAlias.Text = "";
                }
            }
        }

        private void btnRegistration_Click(object sender, EventArgs e)
        {
            HashSet<string> hashs = tRegistration.Tag as HashSet<string>;
            using (HashSetForm hsf = new HashSetForm(hashs))
            {
                if (hsf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    tRegistration.Tag = hsf.Hashs;
                    if (hsf.Hashs != null && hsf.Hashs.Count > 0)
                        tRegistration.Text = JsonTool.ObjectToJson(hsf.Hashs);
                    else
                        tRegistration.Text = "";
                }
            }
        }
    }
}
